package com.example.testdemo.demos.web.controller;

import com.alibaba.excel.util.DateUtils;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.Sheet;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: ExportController
 * @Description:
 * @Date 2024/07/01 17:09
 * @Author songshaoqiu
 **/
@RestController
public class ExportController {

    @ResponseExcel(name = "userDem002", sheets = @Sheet(sheetName = "hhsheet"))
    @GetMapping("/e1")
    public List<DemoData> e1() {
        List<DemoData> dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setUsername("tr1" + i);
            data.setPassword("tr2" + i);
            dataList.add(data);
        }
        return dataList;
    }


    @ResponseExcel()
    @GetMapping("/e2")
    public List<DemoData> e2(String param) throws UnsupportedEncodingException {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // String encodedResult = URLEncoder.encode(name, "UTF-8").replace("+", "%20");
        String format = DateUtils.format(new Date(), DateUtils.DATE_FORMAT_19);
        String name ="测试123 "+format;
        // String name = param;
        name = encodeWithFixedTime(name);
        request.setAttribute("__EXCEL_NAME_KEY__", name);



        List<DemoData> dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setUsername("tr1" + i);
            data.setPassword("tr2" + i);
            dataList.add(data);
        }
        return dataList;
    }

    public static String encodeWithFixedTime(String input) {
        // 寻找时间部分的起始和结束位置
        int timeStartIndex = -1;
        int timeEndIndex = -1;

        // 寻找时间部分的起始位置
        for (int i = 0; i < input.length(); i++) {
            if (Character.isDigit(input.charAt(i)) && input.charAt(i + 1) == ':') {
                timeStartIndex = i-1;
                break;
            }
        }

        // 寻找时间部分的结束位置
        if (timeStartIndex != -1) {
            for (int i = timeStartIndex; i < input.length(); i++) {
                if (input.charAt(i) == ' ' || i == input.length() - 1) {
                    timeEndIndex = i;
                    break;
                }
            }
        }

        // 如果没有找到时间部分的结束位置，则默认到字符串的结尾
        if (timeEndIndex == -1) {
            timeEndIndex = input.length();
        }

        // 提取时间部分
        String timePart = input.substring(timeStartIndex, timeEndIndex);

        try {
            // 对名称部分进行 URL 编码，空格替换为 %20
            String namePart = input.substring(0, timeStartIndex);
            String endNamePart = input.substring(timeEndIndex,input.length());
            String encodedName = URLEncoder.encode(namePart, "UTF-8").replace("+", "%20");
            String endName = URLEncoder.encode(endNamePart, "UTF-8").replace("+", "%20");

            // 拼接编码后的结果，保留时间部分
            return encodedName + timePart + endName;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            // 这里可以根据需要处理异常情况
            return null;
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String originalString = "Hello World";
        String encodedString = URLEncoder.encode(originalString, StandardCharsets.UTF_8.name());
        String correctedString = encodedString.replace("+", "%20");
        System.out.println(correctedString);

    }
}
